import * as React from 'react';
import { createApp } from 'ice';
import LocaleProvider from '@/components/LocaleProvider';
import { getLocale } from '@/utils/locale';
import {Message} from '@alifd/next';
import {getCookieBBT, getBBTKey, getHeaderParam, removeCookieBBT} from '@/utils/cookie'
import Login from '@/pages/Login';
import {API_URL_MAP, BASE_URL} from '@/api/ApiUrl'

const locale = getLocale();
// 请求头
const headerParam ={}
headerParam[getBBTKey()] = getCookieBBT();

const appConfig = {
  app: {
    rootId: 'ice-container',
    addProvider: ({ children }) => {
      return (<LocaleProvider locale={locale}>{children}</LocaleProvider>);
    },
  },
  request: {
    baseURL: BASE_URL,
    headers: getHeaderParam(),
    // 拦截器
    interceptors: {
      request: {
        onConfig: (request) => {
          // console.log('拦截器 request：', request.url)
          // if('/user/login' == request.url){
          //   console.log('拦截器 request：登录请求')
          // }
          return request;
        }
      },
      response: {
        onConfig: (response) => {
          const url = response.config.url;
          // 只拦截配置的后台请求接口
          // console.log("拦截器---- url：", url)
          if(API_URL_MAP.indexOf(url) > -1){
            // console.log("拦截器----response 拦截", url)
            if(response.status == 200){
              const data = response.data;
              if(data && data.status == 10){
                Message.error('****请登录****')
                console.log("拦截器----请登录", url)
                // removeCookieBBT();
                window.location.href ='/'
                return Promise.reject(response);
              }else{
                return response;
              }
            }else{
              console.log("拦截器---- error.response.data")
            }
          }else{
            return response;
          }
        },
        onError: (error) => {
          // 请求出错：服务端返回错误状态码
          console.log(error.response.data);
          console.log(error.response.status);
          console.log(error.response.headers);
          return Promise.reject(error);
        }
      },
    },
  },
};
createApp(appConfig);
